package com.bbcare.department.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.PropertiesUtils;
import com.bbcare.comm.QueryModel;
import com.bbcare.comm.ResultModel;
import com.bbcare.department.plat.entity.EducationContentInfo;
import com.bbcare.department.plat.service.IBsStaticParamService;
import com.bbcare.department.plat.service.IEducationManagerService;

@Controller
@RequestMapping("/webapi/service/education")
public class DepartMentSeviceApiController {
	
	Logger logger = Logger.getLogger(DepartMentSeviceApiController.class); 
	@Autowired
	private IEducationManagerService educationManagerService;
	
	@Autowired
	private IBsStaticParamService bsStaticParamService;
	
	/**
	 * 查询宣教内容详情
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/detail")
	public ResultModel<Map<String,Object>> getEducationDetailForApp(@RequestBody String jsonStr) throws Exception{
		logger.info("in getEducationDetailForApp----arg==" + jsonStr);
		JSONObject jsonEsb = JSON.parseObject(jsonStr);
		JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}
		
		String channel = requestParam.getString("channel");
		if(null == channel || !PropertiesUtils.getProperty("channel").equals(channel)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
    	}
		
        String id = requestParam.getString("educationId");
        if(StringUtils.isEmpty(id)){
        	logger.debug("out getEducationDetail----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，宣教ID不能为空");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id", id);
        map.put("resource","1");//app调用
        EducationContentInfo info = educationManagerService.getEducationContentById(map);
        
        Map<String,Object> rspMap = new HashMap<String,Object>();
        rspMap.put("content", info.getContent());
        rspMap.put("createDate", info.getCreateTime());
        rspMap.put("createUser", info.getCreateAuthor());
        rspMap.put("title", info.getTitle());
        rspMap.put("type", info.getType());
        
        Map<String,Object> paramTemp = new HashMap<String,Object>();
		paramTemp.put("type",Constants.EDUCATION_TYPE_CODE);
        Map<String,String> typeMap =  bsStaticParamService.getStaticParamsForMap(paramTemp);
        rspMap.put("typeName", typeMap.get(info.getType()));
        logger.info("out getEducationDetailForApp----arg==" + rspMap);
		return new ResultModel<Map<String,Object>>(rspMap);
	}
	
	/**
	 * 根据科室Id 查询所有宣教 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/list")
	public QueryModel<Map<String,Object>> getEducationlistForApp(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in getEducationlistByCondition----arg==" + jsonStr);
		JSONObject jsonEsb = JSON.parseObject(jsonStr);
		JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}
		
		String channel = requestParam.getString("channel");
		if(null == channel || !PropertiesUtils.getProperty("channel").equals(channel)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
    	}

        String deptId = requestParam.getString("deptId");// 科室id
        if(StringUtils.isEmpty(deptId)){
        	logger.warn("in getEducationlistByCondition----arg==" + jsonStr);
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "缺少参数科室ID");
        }
        //userId 后续根据规则推送时候 
        Map<String,Object> paramMap = requestParam;
        
        paramMap.put("deptId",deptId);
        List<Map<String,Object>> lists = educationManagerService.getContentlistforApp(paramMap);
        
       /* Map<String,Object> paramTemp = new HashMap<String,Object>();
		paramTemp.put("deptId",deptId);
		paramTemp.put("type",Constants.EDUCATION_TYPE_CODE);
		
        Map<String,String> typeMap =  bsStaticParamService.getStaticParamsForMap(paramTemp);
        Map<String,Object> educationContent = null;
        if(!CollectionUtils.isEmpty(lists)){
        	for(int i = 0;i<lists.size();i++){
        		educationContent = lists.get(i);
        		lists.get(i).put("typeName",typeMap.get(educationContent.get("type")));
            }
        }*/
        Integer count = educationManagerService.getContentsCountByDeptId(paramMap);
		logger.info("out getEducationlistByCondition----arg==" + lists);
		return new QueryModel<Map<String,Object>>(lists,count);
	}
	
	
}
